import React, { Component } from 'react';
import CommonHeader from "../../common/jsx/common_header";
import {Table} from 'antd';
import style from './account.module.css';
import $ from 'jquery';
class Account extends Component {
  constructor(props) {
    super(props);
  }

  state = {
    bread:[
      {name:'网站管理',url:'',p_bread:true},
      {name:'账号管理',url:'/home/account'}
    ],
    title_name:'账号管理',
  }

  componentDidMount() {
    const data = [];
    const that = this;
    $.ajax({
      url:"/login",
      type:'POST',
      dataType:'json',
      data:{
        act:'getalluser'
      },
      success(e){
        var now_time = Math.round(new Date().getTime()/1000).toString()
        // console.log(Math.round(new Date().getTime()/1000).toString())
        for(var i of e.data){
          i.session_id = i.session_id!=""?JSON.parse(i.session_id):""
        }
        const columns = [
          {
            title:'用户',
            dataIndex:'username',
            width:'15%',
          },
          {
            title:'昵称',
            dataIndex:'nickname',
            width:'20%',
          },
          {
            title:'状态',
            dataIndex:'session_id', 
            width:'15%',
            render:(a)=>{return(a.session_id!=""?((parseInt(a.lasttime)+parseInt(65)>=parseInt(now_time))?'登入中':'离线'):"离线")}
          },
          {
            title:'ip',
            dataIndex:'ip',
            width:'25%',
          },
          {
            title:'登入时间',
            dataIndex:'lasttime',
            render:(time)=>(that.changetime(time)),
            sorter:(a,b)=>(parseInt(a.lasttime) - parseInt(b.lasttime)),
            width:'25%',
          },
        ]
        that.setState({
          data:e.data,
          columns
        })
      }
    })
  }

  changetime=(ntime)=>{
    if(ntime.toString().length<=10){
      var time = new Date(parseInt(ntime)*1000)
    }else{
      var time = new Date(ntime)
    }
    return time.getFullYear()+'年'+Math.round(time.getMonth()+1)+'月'+time.getDate()+'日'+time.getHours()+'时'+time.getMinutes()+'分'+time.getSeconds()+'秒';
  }

  render() { 
    return (
      <div>
        <CommonHeader bread={this.state.bread} title_name={this.state.title_name} />
        <div className={style.logger}>
          <div className={style.table}>
            <Table rowKey='id' pagination={this.state.pagination} columns={this.state.columns} dataSource={this.state.data} />
          </div>
        </div>
      </div>
    );
  }
}
 
export default Account;